import { Route, Redirect, RouteProps } from 'react-router-dom';
import { hasToken } from '../../utils/storage';

interface AuthRouteType extends RouteProps {}
export default function AuthRoute({ path, component: Component, ...resProps }: AuthRouteType) {
  if (!Component) {
    console.warn('component 不存在');
    return null;
  }
  return (
    <Route
      {...resProps}
      path={path}
      render={(_props: any) => {
        if (hasToken()) {
          return <Component {..._props} />;
        }
        return <Redirect to={{ pathname: '/login', state: { from: path } }} />;
      }}
    />
  );
}
